package com.sgb.kjwl.view.ui.adapter.self.publish;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.databinding.ItemPublicMyPriceBinding;
import com.sgb.kjwl.model.entity.user.PublicMyPriceListEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.im.immodel.IMUtils;
import com.swgk.core.util.MToast;

import org.json.JSONObject;

import java.util.List;

/**
 * 租赁经营--我的报价--列表适配器
 */
public class PublicMyPriceListAdapter extends BaseRecycleAdapter<PublicMyPriceListEntity> {

    private Context context;
    private String type = "0";

    public PublicMyPriceListAdapter(Context context, List<PublicMyPriceListEntity> datas, String type) {
        super(context, R.layout.item_public_my_price, datas);
        this.context = context;
        this.type = type;
    }

    @Override
    public void convert(ItemViewHolder holder, final PublicMyPriceListEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ItemPublicMyPriceBinding binding = (ItemPublicMyPriceBinding) holder.getmBinding();
        if ("0".equals(type)) {
            binding.tvName.setText("需求方名称：" + entity.getDemandCompanyName());
        } else {
            if (!TextUtils.isEmpty(entity.getCompanyName())) {
                binding.tvName.setText("客户名称：" + entity.getCompanyName());
            } else {
                binding.tvName.setText("客户名称：" + entity.getCreateUserName());
            }

        }

        //（全部=""，0待接受，1已接受，已忽略=2，3已取消）
        switch (entity.getIsAcceptQuote()) {
            case "0"://已报价待接受
                binding.ivQuoteState.setImageResource(R.mipmap.ic_quote_price);
                //状态(1:报价中;3:报价结束;4:已完成)
//                if (entity.getStatus() == 1) {
//                    if("0".equals(type)){
//                        binding.tvStateAccept.setVisibility(View.VISIBLE);
//                    }else{
//                        binding.tvStateCancel.setVisibility(View.VISIBLE);
//                    }
//                } else
                if (entity.getStatus() == 3) {
                    binding.tvStateAccept.setVisibility(View.GONE);
                    binding.tvStateCancel.setVisibility(View.GONE);
                } else if (entity.getStatus() == 4) {
                    binding.tvStateAccept.setVisibility(View.GONE);
                    binding.tvStateCancel.setVisibility(View.GONE);
                } else {
                    if ("0".equals(type)) {
                        binding.tvStateCancel.setVisibility(View.VISIBLE);
                    } else {
                        binding.tvStateAccept.setVisibility(View.VISIBLE);
                    }
                }
                break;
            case "1"://已接受
                binding.ivQuoteState.setImageResource(R.mipmap.ic_quote_accept);
                binding.tvStateAccept.setVisibility(View.GONE);
                binding.tvStateCancel.setVisibility(View.GONE);
//                if (entity.getStatus() == 4 && entity.getCanOperate() == 1) {
//                    binding.tvStateDelete.setVisibility(View.GONE);
//                } else {
//                    binding.tvStateDelete.setVisibility(View.VISIBLE);
//                }
                break;
            case "2"://已忽略
                //旧数据已忽略时，也设置为已取消
                binding.tvStateAccept.setVisibility(View.GONE);
                binding.tvStateCancel.setVisibility(View.GONE);
                binding.ivQuoteState.setImageResource(R.mipmap.ic_quote_cancel);
//                 binding.ivQuoteState.setImageResource(R.mipmap.ic_quote_ignore);
                break;
            case "3"://已取消
                if ("0".equals(type)) {
                    binding.tvHanXun.setVisibility(View.GONE);
                    binding.tvJiaoLiu.setVisibility(View.GONE);
                } else {
                    binding.tvHanXun.setVisibility(View.VISIBLE);
                    binding.tvJiaoLiu.setVisibility(View.VISIBLE);
                }
                binding.tvStateAccept.setVisibility(View.GONE);
                binding.tvStateCancel.setVisibility(View.GONE);
                binding.ivQuoteState.setImageResource(R.mipmap.ic_quote_cancel);
                break;
            default:
                break;
        }
        if (null != onBtnClickListener) {
            binding.tvStateAccept.setOnClickListener(v -> onBtnClickListener.onChangePriceClick(entity, holder.getAdapterPosition()));
            binding.tvStateCancel.setOnClickListener(v -> onBtnClickListener.onCancelClick(entity, holder.getAdapterPosition()));
            binding.tvStateDetail.setOnClickListener(v -> onBtnClickListener.onViewDetailsClick(entity, holder.getAdapterPosition()));
            binding.llItem.setOnClickListener(v -> onBtnClickListener.onItemClick(entity, holder.getAdapterPosition()));
            binding.tvHanXun.setOnClickListener(v -> MToast.showToast(context, "功能开发中"));
            binding.tvJiaoLiu.setOnClickListener(v -> {
                if ("0".equals(type)) {
                    getImByCustomerId(entity.getDemandCreateUserNo());
                } else {
                    getImByCustomerId(entity.getCreateUser());
                }
            });
        }
    }

    private onBtnClickListener onBtnClickListener;

    public void setonBtnClick(onBtnClickListener onBtnClickListener) {
        this.onBtnClickListener = onBtnClickListener;
    }

    public interface onBtnClickListener {
        //接受报价
        void onChangePriceClick(PublicMyPriceListEntity entity, int position);

        //取消报价
        void onCancelClick(PublicMyPriceListEntity entity, int position);

        //查看需求H5
        void onViewDetailsClick(PublicMyPriceListEntity entity, int position);

        //点击item查看报价详情
        void onItemClick(PublicMyPriceListEntity entity, int position);
    }


    //交流获取IM
    public void getImByCustomerId(String customerServiceContactsId) {
        APIManager.getInstance().getLeaseAPI().getImByCustomerId(customerServiceContactsId).enqueue(new ICallBack<BaseEntity>(true, true) {
            @Override
            public void onSuccess(BaseEntity data) {
                try {
                    JSONObject jsonObject = new JSONObject(data.getData().toString());
                    String imAccount = jsonObject.getString("imAccount");
                    IMUtils.getInstance().buildSingleTalking(imAccount);
                } catch (Exception ex) {
                }
            }
        });
    }

}
